/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-17
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.basics.entity.Brand;
import com.rzico.basics.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.util.*;

/**
 * 品牌档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-17
 */
@Api(description = "品牌档案接口")
@RestController
@RequestMapping("/admin/brand")
public class BrandAdminController extends BaseController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询品牌档案
     *
     * @return
    */
    @ApiOperation("分页查询品牌档案")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "品牌名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagIds", value = "标签", dataType = "String", paramType = "query")
    })
    public CommResult<Brand> list(String startDate, String endDate,String name, Long [] tagIds, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (StringUtils.isNotEmpty(startDate)){
            params.put("startDate", startDate);
        }
        if (StringUtils.isNotEmpty(endDate)){
            params.put("endDate", endDate);
        }

        if (name!=null) {
            params.put("name",name);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        params.put("deleted", 0);

        if (tagIds!=null && tagIds.length>0) {
            params.put("tagIds",tagIds);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Brand> list = brandService.selectList(params);
        PageResult<Brand> pageResult = new PageResult<Brand>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条品牌档案
     *
     * @return
    */
    @ApiOperation("查询单条品牌档案")
    @GetMapping("/find/{id}")
    public CommResult<Brand> find(@PathVariable String id) {
        Brand result = brandService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("brand",result);
        return CommResult.success(data);

    }

    /**
     * 保存品牌档案
     *
     * @param brand
     * @return
     */
    @ApiOperation("保存品牌档案")
    @PostMapping("/save")
    @ApiImplicitParams({@ApiImplicitParam(name = "tagIds", value = "标签", dataType = "String", paramType = "query")
    })
    public CommResult<Brand> save(@RequestBody Brand brand,Long [] tagIds) {

        Enterprise enterprise = enterpriseService.getCurrent();
        brand.setCreateDate(new Date());
        brand.setDeleted(false);
        brand.setEnterpriseId(enterprise.getId());
        int affectCount = brandService.save(brand,tagIds);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Brand result = brandService.findById(brand.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除品牌档案
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除品牌档案", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除品牌档案,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Brand> del(@PathVariable String ids) {

        brandService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新品牌档案
     *
     * @param brand
     * @return
     */
    @Log(desc = "更新品牌档案", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新品牌档案")
    @PostMapping("/update")
    @ApiImplicitParams({@ApiImplicitParam(name = "tagIds", value = "标签", dataType = "String", paramType = "query")
    })
    public CommResult<Brand> update(@RequestBody Brand brand,Long [] tagIds) {
        int affectCount = brandService.update(brand,tagIds);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Brand result = brandService.findById(brand.getId());
        return CommResult.success(result);
    }

}
